package org.flexharmony.eclipse.properties.classinclusion;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class SelectJavaSourceDialog extends Dialog implements ISelectionChangedListener, SelectionListener
{
  private IFolder sourceFolder;
  
  private boolean multiMode;
  
  private boolean include;
  
  private TreeViewer treeViewer;
  
  private JavaSourceContentProvider contentProvider;
  
  private Button okButton;
  
  private boolean add;
  
  private List<IResource> selectedResources;
  
  public SelectJavaSourceDialog(Shell parentShell, IFolder sourceFolder, boolean multiMode, boolean include)
  {
    super(parentShell);
    setShellStyle(SWT.RESIZE | SWT.TITLE);
    this.sourceFolder = sourceFolder;
    this.multiMode = multiMode;
    this.include = include;
  }
  
  public boolean isAdd()
  {
    return add;
  }
  
  public List<IResource> getSelectedResources()
  {
    return selectedResources;
  }

  /**
   * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
   */
  public void selectionChanged(SelectionChangedEvent event)
  {
    TreeSelection selection = (TreeSelection)treeViewer.getSelection();
    okButton.setEnabled(!selection.isEmpty());
  }

  /**
   * @see SelectionListener#widgetDefaultSelected(SelectionEvent)
   */
  public void widgetDefaultSelected(SelectionEvent evt)
  {
    buttonPressed(IDialogConstants.OK_ID);
  }
  
  /**
   * @see SelectionListener#widgetSelected(SelectionEvent)
   */
  public void widgetSelected(SelectionEvent evt)
  {}

  protected void configureShell(Shell shell)
  {
    super.configureShell(shell);
    shell.setText(include ? "Inclusion Patterns" : "Exclusion Patterns");
  }
  
  protected Control createDialogArea(Composite parent)
  {
    Composite composite = (Composite) super.createDialogArea(parent);
    GridData gd = new GridData();
    gd.widthHint = 500;
    gd.heightHint = 400;
    composite.setLayoutData(gd);

    Label label = new Label(composite, SWT.NONE);
    String text = multiMode ? "Choose folders or files to" : "Chose a folder or file to";
    text += include ? " include:" : " exclude:";
    label.setText(text);

    contentProvider = new JavaSourceContentProvider();
    int style = multiMode ? SWT.MULTI : SWT.SINGLE;
    treeViewer = new TreeViewer(composite, style);
    treeViewer.setContentProvider(contentProvider);
    treeViewer.setLabelProvider(contentProvider);
    treeViewer.setInput(sourceFolder);
    treeViewer.addSelectionChangedListener(this);
    treeViewer.getTree().addSelectionListener(this);
    GridData treeGridData = new GridData();
    treeGridData.grabExcessVerticalSpace = true;
    treeGridData.grabExcessHorizontalSpace = true;
    treeGridData.verticalAlignment = GridData.FILL;
    treeGridData.horizontalAlignment = GridData.FILL;
    treeViewer.getTree().setLayoutData(treeGridData);

    return composite;
  }

  protected void createButtonsForButtonBar(Composite parent)
  {
    okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    okButton.setEnabled(false);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
  }

  protected void buttonPressed(int buttonId) 
  {
    switch (buttonId)
    {
      case IDialogConstants.OK_ID:
        if (!treeViewer.getSelection().isEmpty())
        {
          selectedResources = new ArrayList<IResource>();
          ITreeSelection selection = (ITreeSelection)treeViewer.getSelection();
          Object[] selectedElements = selection.toArray();
          for (Object selectedElement : selectedElements)
          {
            selectedResources.add((IResource)selectedElement);
          }
          add = true;
        }
        break;
    }
    super.buttonPressed(buttonId);
  }
}
